<?php
namespace Admin\Controller;

use Common\Common\CommonController;
/**
 * 商品分组
 * 
 * @author 兰学宝
 *        
 */
class TagController extends CommonController
{

    private $Model;

    function __construct()
    {
        parent::__construct();
        $this->Model = M('mall_tag');
    }
    
    /**
     * 列表
     */
    public function index()
    {
        if (IS_AJAX) {
            $Model = D('Tag');
            $rows = $Model->getTag($this->shop['id']);
            $this->ajaxReturn($rows);
        }
        $this->assign(array(
            'code' => session('shop.code'),
        ));
        $this->display();
    }
    
    /**
     * 添加分组
     */
    public function add(){
        if(IS_POST){
            $data = I('post.');
            $data['create_time'] = date('Y-m-d H:i:s');
            $result = $this->Model->add($data);
            if($result > 0){
                $data['id'] = $this->Model->getLastInsID();
                $this->success($data);
            }
            $this->error('添加失败！');
        }
        $this->assign(array('data' => array()));
        $this->display('edit');
    }
    
    /**
     * 编辑
     */
    public function edit($id = 0){
        if(IS_POST){
            $data = I('post.');
            if(intval($id) <= 0){
                $this->error('数据ID异常！');
            }
            $result = $this->Model->save($data);
            if($result >= 0){
                $this->success('已修改！');
            }
            $this->error('修改失败！');
        }
        
        $data = $this->Model->find($id);
        if(empty($data)){
            $this->error('数据不存在或已被删除！');
        }
        
        $this->assign(array('data' => $data ));
        $this->display();
    }
    
    /**
     * 删除分组
     */
    public function delete($id = 0){
        $Model = D("Tag");
        $result = $Model->delTage($id);
        
        if($result < 0){
            $this->error($Model->getError());
        }
        
        $this->success('删除成功！');
    }
}

?>